package com.zdkj.daemon.quartz.util;

import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.daemon.quartz.entity.SysJob;
import com.zdkj.daemon.quartz.exception.TaskException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 定时任务rest Inner反射实现
 *
 * @author 郑健楠
 * @Description: <p>
 * 如: http://zdkj-subway-support/swstoperatelog/task/...
 * </p>
 */
@Slf4j
@Component("restInnerTaskInvok")
@AllArgsConstructor
public class RestInnerTaskInvok implements ITaskInvok {

    private RestTemplate restTemplate;
    /*
     * 反射方法(non-Javadoc)
     * @see com.zdkj.daemon.quartz.util.ITaskInvok#invokMethod(com.zdkj.daemon.quartz.entity.SysJob)
     */
    @Override
    public void invokMethod(SysJob sysJob) throws TaskException {
        R r = restTemplate.getForObject(sysJob.getExecutePath(), R.class);
        if (CommonConstants.FAIL == r.getCode()) {
            log.error("定时任务restTaskInvok异常,执行任务：{}", sysJob.getExecutePath());
            throw new TaskException("定时任务restTaskInvok业务执行失败,任务：" + sysJob.getExecutePath());
        }
    }
}
